﻿using CoffeeShop.Models.Entities;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;

namespace CoffeeShop.DAO
{
    public class TableDao
    {
        private CFMSEntities _entities = new CFMSEntities();

        /// <summary>
        /// Get all table
        /// </summary>
        /// <returns></returns>
        public List<Table> GetAllTables()
        {
            try
            {
                var resultQuery = from table in _entities.Tables
                                  where table.IsDeleted == false
                                  select table;
                return resultQuery.ToList();
            }
            catch (Exception ex)
            {
                System.Console.WriteLine(ex.Message);
                return null;
            }
        }

        /// <summary>
        /// Get table by TableId
        /// </summary>
        /// <param name="tableId">TableId</param>
        /// <returns>Table object</returns>
        public Table GetTableById(int tableId)
        {
            try
            {
                var resultQuery = (from table in _entities.Tables
                                   where table.TableId == tableId
                                   select table).FirstOrDefault();
                return resultQuery;
            }
            catch (Exception ex)
            {
                System.Console.WriteLine(ex.Message);
                return null;
            }
        }

        /// <summary>
        /// Get a Table by TableName, OfficeId, AreaId
        /// </summary>
        /// <param name="tableName">TableName</param>
        /// <param name="officeId">OfficeId</param>
        /// <param name="areaId">AreaId</param>
        /// <returns>Table object</returns>
        public Table GetTableOfOffice(string tableName, int officeId, int areaId)
        {
            try
            {
                var resultSelect = (from table in _entities.Tables
                                    where table.Name == tableName
                                          && table.OfficeId == officeId
                                          && table.AreaId == areaId
                                          && table.IsDeleted == false
                                    select table).FirstOrDefault();
                return resultSelect;
            }
            catch (Exception ex)
            {
                System.Console.WriteLine(ex.Message);
                return null;
            }
        }

        /// <summary>
        /// Create a new table
        /// </summary>
        /// <param name="table">Table object</param>
        /// <returns>True: Success --- False: Fail</returns>
        public bool CreateTable(Table table)
        {
            try
            {
                _entities.Tables.Add(table);
                var resultAdd = _entities.SaveChanges();
                if (resultAdd > 0)
                {
                    return true;
                }
                return false;
            }
            catch (Exception ex)
            {
                System.Console.WriteLine(ex.Message);
                return false;
            }
        }

        /// <summary>
        /// Update a table
        /// </summary>
        /// <param name="table">Table object</param>
        /// <returns>True: Success --- False: Fail</returns>
        public bool UpdateTable(Table table)
        {
            try
            {
                _entities.Entry(table).State = EntityState.Modified;
                var resultUpdate = _entities.SaveChanges();
                if (resultUpdate > 0)
                {
                    return true;
                }
                return false;
            }
            catch (Exception ex)
            {
                System.Console.WriteLine(ex.Message);
                return false;
            }
        }

        /// <summary>
        /// Delete a table
        /// </summary>
        /// <param name="tableId">TableId</param>
        /// <returns>True: Success --- False: Fail</returns>
        public bool DeleteTable(int tableId)
        {
            try
            {
                var selectTable = (from table in _entities.Tables
                                   where table.TableId == tableId
                                   select table).FirstOrDefault();
                selectTable.IsDeleted = true;
                _entities.Entry(selectTable).State = EntityState.Modified;
                var resultDelete = _entities.SaveChanges();
                if (resultDelete > 0)
                {
                    return true;
                }
                return false;
            }
            catch (Exception ex)
            {
                System.Console.WriteLine(ex.Message);
                return false;
            }
        }

        /// <summary>
        /// Check is duplicate Table when update a table
        /// </summary>
        /// <param name="tableId">TableId</param>
        /// <param name="tableName">TableName</param>
        /// <param name="officeId">OfficeId</param>
        /// <param name="areaId">AreaId</param>
        /// <returns>True: Existed --- False: Not Existed</returns>
        public bool IsDuplicateTableWhenUpdate(int tableId, string tableName, int officeId, int areaId)
        {
            try
            {
                var selectTable = (from table in _entities.Tables
                                   where table.TableId != tableId
                                   && table.Name == tableName
                                   && table.OfficeId == officeId
                                   && table.AreaId == areaId
                                   select table).FirstOrDefault();
                if (selectTable != null)
                {
                    return true;
                }
                else
                {
                    return false;
                }
            }
            catch (Exception ex)
            {
                System.Console.WriteLine(ex.Message);
                return false;
            }
        }
    }
}